using System;
using System.Data;
using System.Collections;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// Summary description for ListBoxHelper
/// </summary>
public partial class ControlHelper
{
    public void FillDataTableToListBox(
    ListBox listBox, 
    string displayMember, 
    string valueMember,
    string selectedValue)
    {
                 
        DataTable dataTable =
        sqlServerProvider.GetDataTable();
        if (dataTable != null)
        {
            listBox.DataSource = dataTable;
            listBox.DataTextField = displayMember;
            listBox.DataValueField = valueMember;
        }
        else
            listBox.DataSource = null;
        listBox.DataBind();
        if (selectedValue != "")
            listBox.SelectedValue = selectedValue;
        
    }
    public void FillDataTableToListBox(
    ListBox listBox, 
    string firstDisplayMember, 
    string firstValueMember, 
    string displayMember, 
    string valueMember,
    string selectedValue)
    {
        
        
        DataTable dataTable =
        sqlServerProvider.GetDataTable();

        if (dataTable != null)
        {
            if (firstDisplayMember != "")
            {
                DataRow dataRow = dataTable.NewRow();
                dataRow[0] = firstDisplayMember;
                dataRow[1] = firstValueMember;
                dataTable.Rows.InsertAt(dataRow, 0);
            }
            listBox.DataTextField = displayMember;
            listBox.DataValueField = valueMember;
            listBox.DataSource = dataTable;
            listBox.DataBind();
            if (selectedValue != "")
                listBox.SelectedValue = selectedValue;
        }
        else
        {
            listBox.Items.Add(
            new ListItem(firstDisplayMember, 
            firstValueMember));
        }

        
    }
    void FillStringToListBox(
    ListBox listBox,    
    string firstDisplayMember,
    string firstValueMember)
    {
        if (firstDisplayMember != "")
        {
            listBox.Items.Add(
            new ListItem(firstDisplayMember,
            firstValueMember));
        }
        string[] valueCollection =
        sqlServerProvider.GetStrings();
        for (int i = 0; i < valueCollection.Length; i++)
        {
            listBox.Items.Add(
            valueCollection[i]);
        }

    }
    
    public void FillArrayListToListBox(
    ListBox listBox, 
    string firstDisplayMember, 
    string firstValueMember,
    string selectedValue)
    {
        
         
        ArrayList arrayList =
        sqlServerProvider.GetArrayList();
        if (arrayList.Count != 0)
        {
            FillArrayListToListBox(
            listBox, arrayList, 
            firstDisplayMember, firstValueMember);
            if (selectedValue != "")
                listBox.SelectedValue = selectedValue;
        }

        
    }
    void FillArrayListToListBox(
    ListBox listBox, 
    ArrayList arrayList, 
    string firstDisplayMember, 
    string firstValueMember)
    {
        if (firstDisplayMember != "")
        {
            listBox.Items.Add(
            new ListItem(firstDisplayMember, 
            firstValueMember));
        }
        for (int i = 0; i < arrayList.Count; i++)
        {
            string[] item = (string[])arrayList[i];
            listBox.Items.Add(
            new ListItem(item[1], item[0]));
        }

    }
}
